package com.qf.controller;


import com.qf.confirm.MyConfirmCallback;
import com.qf.confirm.MyReturnCallBack;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MyConfirmCallback confirmCallback;
    @Autowired
    private MyReturnCallBack returnCallBack;

    @RequestMapping("send1")
    public String send1(){
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.convertAndSend("exchange_direct_demo01","item.insert","hello insert");
        return "OK";
    }

    @RequestMapping("send2")
    public String send2(){
        //设置回调函数
        rabbitTemplate.setReturnCallback(returnCallBack);
        //发送消息
        rabbitTemplate.convertAndSend("exchange_direct_demo01","item.insert123","hello insert");
        return "OK";
    }

    @RequestMapping("send3")
    public String send3(){
        //设置回调函数
        rabbitTemplate.setReturnCallback(returnCallBack);
        //发送消息
        rabbitTemplate.convertAndSend("exchange_direct_demo01","item.insert","hello insert");
        return "OK";
    }

    /**
     * 发送 ttl测试相关的消息
     * @return
     */
    @RequestMapping("/send4")
    public String send4(){
        rabbitTemplate.convertAndSend("exchange_direct_ttl_demo","item.ttl","hello ttl哈哈");
        return "OK";
    }

    /**
     * 测试发送死信队列
     * @return
     */
    @RequestMapping("/send5")
    public String send5() {
        //发送消息到死信队列   可以使用默认的交换机 指定ourtingkey为死信队列名即可
        rabbitTemplate.convertAndSend("dlx_queue_demo02", "hello dlx哈哈");
        return "ok";
    }

    /**
     * 发送下单
     *
     * @return
     */
    @RequestMapping("/send6")
    public String send6(){
        //发送消息到死信队列   可以使用默认的交换机 指定routingkey为死信队列名即可
        System.out.println("用户下单成功，10秒钟之后如果没有支付，则过期，回滚订单");
        System.out.println("时间："+new Date());
        return "OK";
    }
}
